﻿
$(document).ready(function () {
   

    jQuery.ajax = (function (_ajax) {

        var protocol = location.protocol,
            hostname = location.hostname,
            exRegex = RegExp(protocol + '//' + hostname),
            YQL = 'http' + (/^https/.test(protocol) ? 's' : '') + '://query.yahooapis.com/v1/public/yql?callback=?',
            query = 'select * from html where url="{URL}" and xpath="*"';

        function isExternal(url) {
            return !exRegex.test(url) && /:\/\//.test(url);
        }

        return function (o) {

            var url = o.url;

            if (/get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url)) {

                // Manipulate options so that JSONP-x request is made to YQL

                o.url = YQL;
                o.dataType = 'json';

                o.data = {
                    q: query.replace(
                        '{URL}',
                        url + (o.data ?
                            (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
                        : '')
                    ),
                    format: 'xml'
                };

                // Since it's a JSONP request
                // complete === success
                if (!o.success && o.complete) {
                    o.success = o.complete;
                    delete o.complete;
                }

                o.success = (function (_success) {
                    return function (data) {

                        if (_success) {
                            // Fake XHR callback.
                            _success.call(this, {
                                responseText: data.results[0]
                                    // YQL screws with <script>s
                                    // Get rid of them
                                    .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                            }, 'success');
                        }

                    };
                })(o.success);

            }

            return _ajax.apply(this, arguments);

        };

    })(jQuery.ajax);

  $.get('http://vpp24h.com/Ads/src/tempCode.html?id=2', function (res) { //get the html source of this website

      

      //tao 1 bien chinh luu tru html 
      var fullHtml = "";
      var utm_source = '?utm_source=' + otpvpp.utm_source;
      var utm_campaign = '&utm_campaign=' + otpvpp.utm_campain;
      var utm_medium = '&utm_medium=' + otpvpp.utm_medium;
      var utm_Full = utm_source + utm_campaign + utm_medium;
      //loc a link va a title
      $(res.responseText).find('.topname h2').each(function () { //loop though all h3 in the snippets list
          var anchor = $(this).children('a:last'); //get the actual link with the text inside
          var testDiv = '<div class="collect"><div class="item"><div class="hotContentTop"><div class="topname"><h2><a target="_blank" href="Vpp24h.link.href">Vpp24h.H2.Title</a></h2></div><div class="topimg"><a title="Vpp24h.H2.Title" href="Vpp24h.link.href" target="_blank"><img alt="Vpp24h.link.href" src="Vpp24h.Image.URL" /></a></div><div class="hotPrice"><a href="Vpp24h.link.href" target="_blank"><span class="price">Vpp24h.hotPrice.Price</span></a><div><a href="Vpp24h.link.href" class="btnXem" target="_blank">Xem</a></div><div class="textOnly"></div></div></div></div></div>';

          //<a href="Css Tricks" target="_blank">Hộp phấm trang điểm</a>
          
          
          
          var testDiv2 = testDiv.replace(/Vpp24h.link.href/g, anchor.attr('href') + utm_Full).replace(/Vpp24h.H2.Title/g, anchor.text());
          //testDiv2 = testDiv.replace(/Vpp24h.hotPrice.href/g, anchor.attr('href'));

          

          fullHtml += testDiv2;
          
      });

      //loc a link va hinh anh
      $(res.responseText).find('.topimg a').each(function () { //loop though all h3 in the snippets list
          var anchor = $(this).children('img:last'); //get the actual link with the text inside
          var testDiv = fullHtml;

          //<a href="Css Tricks" target="_blank">Hộp phấm trang điểm</a>
          var testDiv2 = testDiv.replace("Vpp24h.Image.URL", anchor.attr('src'));
          fullHtml = testDiv2;

      });

      //loc a gia
      $(res.responseText).find('.hotPrice a:first').each(function () { //loop though all h3 in the snippets list

          var textGia = $(this).children('span'); //get the actual link with the text inside
          var testDiv = fullHtml;
          
          //<a href="Css Tricks" target="_blank">Hộp phấm trang điểm</a>
          var testDiv2 = testDiv.replace("Vpp24h.hotPrice.Price", textGia.text());

          fullHtml = testDiv2;

      });
      
      var fullCode = '<link rel="stylesheet" type="text/css" href="http://vpp24h.com/Ads/js/style.css" media="all" /><div id="page-wrap"> </div>    <div id="vpp24hads">        <div id="vpp24h-box-cover" class="vpp24h-box-cover-left">            <div class="close">                <a class="close_1" href="javascript:void(0)" onclick="vpp24hHide()" >&times;</a>                <a class="reopen_1" href="javascript:void(0)" onclick="vpp24hShow()">&and;</a>            </div>            <div class="condata">                <div class="conshow">                    <div id="vpp24h-container">                        <div class="products vertical" id="products">                            <!-- root element for the items -->                            <div id="productsListAds" style="width:300px;" class="items">/*AdsListContent*/</div>     <div class="clear"></div>                      </div>                        <div class="control_button" style="display: none;">                            <div style="display: block; width: 12px; margin: 0px 0px 10px 4px;">                                <div class="prev up"></div>                                <div class="next down"></div>                                <div class="clear"></div>                            </div>                            <a href="http://vpp24h.com/" class="vertical-text" target="blank">Xem thêm tại VPP24H.COM</a>                        </div>                        <div style="clear: both"></div>                        <div>                            <div class="hcontrol">                                <a class="prev browse a-left"></a><a class="next browse a-right"></a>                            </div>                            <a class="primary-link" target="blank" href="http://vpp24h.com/">Giao hàng thu tiền tận nơi trên toàn quốc</a>                        </div>                    </div>                </div>                <div class="conhide">                    <div class="marquee" style="display: block;">                        <div class="re-up"></div>                        <div class="re-close"></div>                        <div class="re-logo"></div>                        <div style="display:inline-block; width: 195px; height: 20px;">                            <marquee direction="left" scrollamount="2" scrolldelay="10" style="padding-bottom:5px; margin-left:47px;">Van phòng phẩm - Quà tặng - Ðồ choi</marquee></div></div></div></div></div></div><div id="hddc"></div>';
      
      fullCode = fullCode.replace("/*AdsListContent*/", fullHtml);

      //$('#vpp24hAdsFull').html(fullCode);

      $('#vpp24hAdsFull').html(fullCode); //append it to a list



      




      try {
          var _myc = $(".items"); var _temn = $("#hddc");
          var len = $(".collect", ".items").size();
          while (len > 0) {
              var newidx = Math.floor(Math.random() * len); $(".items .collect").eq(newidx).appendTo(_temn); len = len - 1;
          }
          _myc.html(_temn.html()); _temn.html("");
      }
      catch (error) { };

      $("#products").scrollable({ circular: true, vertical: true, speed: 400 }).autoscroll({ autoplay: true, interval: 3000 }); //3s
  });

  

});

function vpp24hHide() {
    $("#vpp24h-box-cover .condata .conshow").hide();
    $("#vpp24h-box-cover .condata .conhide").show();
    $("#vpp24h-box-cover .close_1").hide();
    $("#vpp24h-box-cover .reopen_1").css({ 'display': 'block' });
}

function vpp24hShow() {
    $("#vpp24h-box-cover .condata .conshow").show();
    $("#vpp24h-box-cover .condata .conhide").hide();
    $("#vpp24h-box-cover .close_1").show();
    $("#vpp24h-box-cover .reopen_1").hide();
}